﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.AccountingObjectCategory;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter.CountryRegion;
using WindowsFormsPresenter.District;
using WindowsFormsPresenter.Province;
using WindowsFormsPresenter.AccountingObject;

namespace WindowsFormApplication.FormCategoryTreeView
{
    public partial class frmVendorListDetail : frmBaseTreeViewDetail, IAccountingObjectCategoriesView, ICountryRegionsView, IDistrictsView, IProvincesView, IAccountingObjectView
    {
        private AccountingObjectCategoriesPresenter _accountingObjectCategoriesPresenter;
        private CountryRegionsPresenter _countryRegionsPresenter;
        private DistrictsPresenter _districtsPresenter;
        private ProvincesPresenter _provincesPresenter;
        private AccountingObjectPresenter _accountingObjectPresenter;

        public IList<AccountingObjectCategoryModel> AccountingObjectCategories
        {
            set
            {
                var accountingObjectCategories = value;

                cboAccountingObjectCategoryId.DataSource = accountingObjectCategories;
                cboAccountingObjectCategoryId.DisplayMember = "AccountingObjectCategoryName";
                cboAccountingObjectCategoryId.ValueMember = "AccountingObjectCategoryID";

                for (int i = 0; i < cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns.Count; i++)
                {
                    cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                }

                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["AccountingObjectCategoryCode"].Hidden = false;
                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["AccountingObjectCategoryName"].Hidden = false;
                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["AccountingObjectCategoryCode"].Header.Caption = "Mã loại NCC";
                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["AccountingObjectCategoryName"].Header.Caption = "Tên loại NCC";
                cboAccountingObjectCategoryId.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";
                if (accountingObjectCategories.Count > 0)
                    cboAccountingObjectCategoryId.Rows[0].Selected = true;
            }
        }
        public IList<CountryRegionModel> CountryRegions
        {
            set
            {
                var countryRegions = value;

                cboCountryRegion.DataSource = countryRegions;
                cboCountryRegion.DisplayMember = "CountryRegionName";
                cboCountryRegion.ValueMember = "CountryRegionCode";

                for (int i = 0; i < cboCountryRegion.DisplayLayout.Bands[0].Columns.Count; i++)
                {
                    cboCountryRegion.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                }

                cboCountryRegion.DisplayLayout.Bands[0].Columns["CountryRegionCode"].Hidden = false;
                cboCountryRegion.DisplayLayout.Bands[0].Columns["CountryRegionName"].Hidden = false;
                cboCountryRegion.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                cboCountryRegion.DisplayLayout.Bands[0].Columns["CountryRegionCode"].Header.Caption = "Mã quốc gia";
                cboCountryRegion.DisplayLayout.Bands[0].Columns["CountryRegionName"].Header.Caption = "Tên quốc gia";
                cboCountryRegion.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";

                if (countryRegions.Count > 0)
                    cboCountryRegion.Rows[0].Selected = true;
            }
        }
        public IList<DistrictModel> Districts
        {
            set
            {
                var districts = value;

                cboDistrict.DataSource = districts;
                cboDistrict.DisplayMember = "DistrictName";
                cboDistrict.ValueMember = "DistrictCode";

                for (int i = 0; i < cboDistrict.DisplayLayout.Bands[0].Columns.Count; i++)
                {
                    cboDistrict.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                }

                cboDistrict.DisplayLayout.Bands[0].Columns["DistrictCode"].Hidden = false;
                cboDistrict.DisplayLayout.Bands[0].Columns["DistrictName"].Hidden = false;
                cboDistrict.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                cboDistrict.DisplayLayout.Bands[0].Columns["DistrictCode"].Header.Caption = "Mã quận, huyện";
                cboDistrict.DisplayLayout.Bands[0].Columns["DistrictName"].Header.Caption = "Tên quận, huyện";
                cboDistrict.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";

                if (districts.Count > 0)
                    cboDistrict.Rows[0].Selected = true;
            }
        }
        public IList<ProvinceModel> Provinces
        {
            set
            {
                var provinces = value;

                cboProvince.DataSource = provinces;
                cboProvince.DisplayMember = "ProvinceName";
                cboProvince.ValueMember = "ProvinceCode";

                for (int i = 0; i < cboProvince.DisplayLayout.Bands[0].Columns.Count; i++)
                {
                    cboProvince.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                }

                cboProvince.DisplayLayout.Bands[0].Columns["ProvinceCode"].Hidden = false;
                cboProvince.DisplayLayout.Bands[0].Columns["ProvinceName"].Hidden = false;
                cboProvince.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                cboProvince.DisplayLayout.Bands[0].Columns["ProvinceCode"].Header.Caption = "Mã tỉnh, thành phố";
                cboProvince.DisplayLayout.Bands[0].Columns["ProvinceName"].Header.Caption = "Tên quận, huyện";
                cboProvince.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";

                if (provinces.Count > 0)
                    cboProvince.Rows[0].Selected = true;
            }
        }

        #region Fields

        public string AccountingObjectID { get; set; }

        public string AccountingObjectCode
        {
            get { return txtAccountingObjectCode.Text; }
            set { txtAccountingObjectCode.Text = value; }
        }

        public string AccountingObjectName
        {
            get { return txtAccountingObjectName.Text; }
            set { txtAccountingObjectName.Text = value; }
        }

        public string AccountingObjectCategoryId
        {
            get { return cboAccountingObjectCategoryId.Value.ToString(); }
            set { cboAccountingObjectCategoryId.Value = value; }
        }

        public string RestaurantId { get; set; }

        public string Prefix
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtPrefix.Text;
                else
                    return null;
            }
            set { txtPrefix.Text = value; }
        }

        public string Address
        {
            get { return txtAddress.Text; }
            set { txtAddress.Text = value; }
        }

        public string Tel
        {
            get { return txtTel.Text; }
            set { txtTel.Text = value; }
        }

        public string Fax
        {
            get { return txtFax.Text; }
            set { txtFax.Text = value; }
        }

        public string EmailAddress
        {
            get { return txtEmailAddress.Text; }
            set { txtEmailAddress.Text = value; }
        }

        public string Website
        {
            get { return txtWebsite.Text; }
            set { txtWebsite.Text = value; }
        }

        public string CompanyTaxCode
        {
            get { return txtCompanyTaxCode.Text; }
            set { txtCompanyTaxCode.Text = value; }
        }

        public DateTime BirthDate
        {
            get { return DateTime.Parse("1/1/1975"); }
            set { var dateTime = value; }
        }

        public DateTime FiredDate
        {
            get { return DateTime.Parse("1/1/1975"); }
            set { var dateTime = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public string ContactName
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtContactName.Text;
                else
                    return null;
            }
            set { txtContactName.Text = value; }
        }

        public string JobCandidateName
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtJobCandidateName.Text;
                else
                    return null;
            }
            set { txtJobCandidateName.Text = value; }
        }

        public bool ContactSex
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                {
                    if (rndContactSex.CheckedIndex == 0) { return true; }
                    else { return false; }
                }
                else
                    return true;
            }
            set
            {
                var contactSex = value;
                if (contactSex == true) { rndContactSex.CheckedIndex = 0; }
                else { rndContactSex.CheckedIndex = 1; }
            }
        }

        public string ContactMobile
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtContactMobile.Text;
                else
                    return null;
            }
            set { txtContactMobile.Text = value; }
        }

        public string ContactEmail
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtContactEmail.Text;
                else
                    return null;
            }
            set { txtContactEmail.Text = value; }
        }

        public string ContactHomeTel
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtContactHomeTel.Text;
                else
                    return null;
            }
            set { txtContactHomeTel.Text = value; }
        }

        public string ContactAddress
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtContactAddress.Text;
                else
                    return null;
            }
            set { txtContactAddress.Text = value; }
        }

        public bool IsPersonal
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 1) { return true; }
                else { return false; }
            }
            set
            {
                var isPersonal = value;
                if (isPersonal == true) { rndIsPersonal.CheckedIndex = 1; }
                else { rndIsPersonal.CheckedIndex = 0; }
            }
        }

        public string IdentificationNumber
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtIdentificationNumber.Text;
                else
                    return null;
            }
            set { txtIdentificationNumber.Text = value; }
        }

        public DateTime IssueDate
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                {
                    if (dtIssueDate.Value != null)
                        return DateTime.Parse(dtIssueDate.Value.ToString());
                    else
                        return DateTime.Parse(dtIssueDate.MinDate.ToString());
                }
                else
                    return DateTime.Parse(dtIssueDate.MinDate.ToString());
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtIssueDate.Value = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtIssueDate.Value = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string IssueBy
        {
            get
            {
                if (rndIsPersonal.CheckedIndex == 0)
                    return txtIssueBy.Text;
                else
                    return null;
            }
            set { txtIssueBy.Text = value; }
        }

        public bool IsVendor
        {
            get { return false; }
            set { var isVendor = value; }
        }

        public bool IsCustomer
        {
            get { return false; }
            set { var isCustomer = value; }
        }

        public bool IsEmployess
        {
            get { return false; }
            set { var isEmployess = value; }
        }

        public string CountryRegion
        {
            get { return cboCountryRegion.Value.ToString(); }
            set { cboCountryRegion.Value = value; }
        }

        public string Province
        {
            get { return cboProvince.Value.ToString(); }
            set { cboProvince.Value = value; }
        }

        public string District
        {
            get { return cboDistrict.Value.ToString(); }
            set { cboDistrict.Value = value; }
        }

        public string Village
        {
            get { return txtVillage.Text.ToString(); }
            set { txtVillage.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }

        #endregion

        protected override void InitData()
        {
            _accountingObjectCategoriesPresenter.DisplayActive(false);
            _countryRegionsPresenter.DisplayActive();
            _districtsPresenter.DisplayActive();
            _provincesPresenter.DisplayActive();
            if (KeyValue != null)
                _accountingObjectPresenter.Display(KeyValue);
        }

        protected override void LoadFormDetail()
        {
            base.LoadFormDetail();
        }

        protected override bool ValidData()
        {
            if ((string.IsNullOrEmpty(AccountingObjectCode)))
            {
                ShowMessageBox("Bạn chưa nhập mã nhà cung cấp !", "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountingObjectCode.Focus();
                return false;
            }
            if ((string.IsNullOrEmpty(AccountingObjectName)))
            {
                ShowMessageBox("Bạn chưa nhập tên nhà cung cấp !", "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountingObjectName.Focus();
                return false;
            }
            if ((string.IsNullOrEmpty(AccountingObjectCategoryId)))
            {
                ShowMessageBox("Bạn chưa nhập loại nhà cung cấp !", "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cboAccountingObjectCategoryId.Focus();
                return false;
            }
            return true;
        }

        protected override bool SaveData()
        {
            try
            {
                if (eActionMode == ActionMode.AddNew)
                {
                    _accountingObjectPresenter.Save();
                    ShowMessageBox("Thêm mới dữ liệu thành công !", "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    _accountingObjectPresenter.Save();
                    ShowMessageBox("Sửa dữ liệu thành công !", "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (ApplicationException ex)
            {
                ShowMessageBox(ex.Message, "Thông báo thay đổi dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        public frmVendorListDetail()
        {
            InitializeComponent();

            _accountingObjectCategoriesPresenter = new AccountingObjectCategoriesPresenter(this);
            _countryRegionsPresenter = new CountryRegionsPresenter(this);
            _districtsPresenter = new DistrictsPresenter(this);
            _provincesPresenter = new ProvincesPresenter(this);
            _accountingObjectPresenter = new AccountingObjectPresenter(this);
        }

        private void rndIsPersonal_ValueChanged(object sender, EventArgs e)
        {
            if (rndIsPersonal.CheckedIndex == 0)
            {
                groupContactInfo.Visible = true;
                this.Size = new System.Drawing.Size(804, 779);
            }
            else
            {
                groupContactInfo.Visible = false;
                this.Size = new System.Drawing.Size(804, 550);
            }
        }
    }
}
